package com.kwd.news.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kwd.news.dao.GlobalConfigDao;
import com.kwd.news.dao.LmCmsAreasDao;
import com.kwd.news.model.GlobalConfig;
import com.kwd.news.model.LmCmsAreas;
import com.kwd.news.model.LmCmsMobile;
import com.kwd.news.po.Area;
import com.kwd.news.po.City;
import com.kwd.news.po.CityPicker;
import com.kwd.news.service.LmCmsMobileService;
import com.meirengu.common.RedisClient;
import com.meirengu.common.StatusCode;
import com.meirengu.model.Result;
import com.meirengu.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * LmCmsMobile服务实现层
 *
 * @author Mao
 * @create Thu May 17 19:50:49 CST 2018
 */
@Service
public class LmCmsMobileServiceImpl extends BaseServiceImpl<LmCmsMobile> implements LmCmsMobileService {

    @Autowired
    private LmCmsAreasDao lmCmsAreasDao;
    @Autowired
    private GlobalConfigDao globalConfigDao;

    @Autowired
    private RedisClient redisClient;

    public static final String CITYCODEHASH = "CITYCODEHASH";
    public static final String CITYNAMEHASH = "CITYNAMEHASH";

    @Override
    public void checkarea() {
        List<Map<String, Object>> aList = getList(new HashMap<>());
        if (aList != null && aList.size() > 0) {
            for (Map<String, Object> map : aList) {
                String province = map.get("province").toString();
                String provinceCode = map.get("provinceCode").toString();
                Map<String, Object> provinceParam = new HashMap<>();
                provinceParam.put("name", province);
                provinceParam.put("code", provinceCode);
                provinceParam.put("levelType", 1);
                List<Map<String, Object>> provinceList = lmCmsAreasDao.getByPage(provinceParam);
                if (provinceList == null || provinceList.size() != 1) {
                    addErrorLog(province, "省错误", provinceCode);
                }


                String city = map.get("city").toString();
                String cityCode = map.get("cityCode").toString();
                Map<String, Object> cityParam = new HashMap<>();
                cityParam.put("name", city);
                cityParam.put("code", cityCode);
                List<Map<String, Object>> cityList = lmCmsAreasDao.getByPage(cityParam);
                if (cityList == null || cityList.size() != 1) {
                    addErrorLog(city, "市错误", cityCode);
                }

                Map<String, Object> cityArea = cityList.get(0);
                String parentCode = cityArea.get("parentCode").toString();
                if (!parentCode.equals(provinceCode)) {
                    Map<String, Object> cityParam2 = new HashMap<>();
                    cityParam2.put("code", parentCode);
                    List<Map<String, Object>> cityList2 = lmCmsAreasDao.getByPage(cityParam2);
                    if (cityList2 == null || cityList2.size() != 1) {
                        addErrorLog(city, "2级市错误", parentCode);
                    } else {
                        Map<String, Object> cityArea2 = cityList2.get(0);
                        String parentCode2 = cityArea2.get("parentCode").toString();
                        if (!parentCode2.equals(provinceCode)) {
                            addErrorLog(city, "2级市父级错误", parentCode);
                        }
                    }
                }

            }
        }
    }

    public void addErrorLog(String city, String value, String phone) {
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setGlobalKey(city);
        globalConfig.setGlobalValue(value);
        globalConfig.setChannel(phone);
        globalConfigDao.insert(globalConfig);
    }

    @Override
    public void warpJson() {
        Map<String, Map<String, String>> data = new HashMap<>();


        List<Map<String, Object>> aList = getList(new HashMap<>());
        if (aList != null && aList.size() > 0) {
            for (Map<String, Object> map : aList) {
                String province = map.get("province").toString();
                String provinceCode = map.get("provinceCode").toString();
                String city = map.get("city").toString();
                String cityCode = map.get("cityCode").toString();

                if (data.containsKey(provinceCode)) {
                    Map<String, String> cityMap = data.get(provinceCode);
                    cityMap.put(cityCode, city);
                } else {
                    Map<String, String> cityMap = new HashMap<>();
                    cityMap.put(cityCode, city);
                    data.put(provinceCode, cityMap);
                }
            }
        }
        int i = 1;
        int j = 1;
        List<CityPicker> list = new ArrayList<>();
        for (Map.Entry<String, Map<String, String>> entry : data.entrySet()) {

            String provinceCode = entry.getKey();
            Map<String, Object> provinceParam = new HashMap<>();
            provinceParam.put("code", provinceCode);
            provinceParam.put("levelType", 1);
            List<Map<String, Object>> provinceList = lmCmsAreasDao.getByPage(provinceParam);
            String province = provinceList.get(0).get("shortName").toString();
            CityPicker provincePicker = new CityPicker();
            provincePicker.setCode(provinceCode);
            provincePicker.setName(province);


            List<CityPicker> cityList = new ArrayList<>();
            for (Map.Entry<String, String> entry2 : entry.getValue().entrySet()) {

                CityPicker cityPicker = new CityPicker();
                cityPicker.setCode(entry2.getKey());
                cityPicker.setName(entry2.getValue());
                cityPicker.setSub(new String[0]);
                cityList.add(cityPicker);
            }
            Collections.sort(cityList);
            provincePicker.setSub(cityList.toArray());
            list.add(provincePicker);
        }
        Collections.sort(list);
        System.out.println(JSON.toJSONString(list));


    }

    @Override
    public void warpJson2() {
        List<Area> cityList = new ArrayList<>();


        List<Map<String, Object>> aList = getList(new HashMap<>());
        if (aList != null && aList.size() > 0) {
            for (Map<String, Object> map : aList) {
                String province = map.get("province").toString();
                String provinceCode = map.get("provinceCode").toString();
                String city = map.get("city").toString();
                String cityCode = map.get("cityCode").toString();

                Area area = new Area();
                area.setId(cityCode);
                area.setValue(city);
                area.setParentId(provinceCode);
                cityList.add(area);

            }
        }

        System.out.println(JSON.toJSONString(cityList));
    }

    @Override
    public void warpJson3() {
        Map<String, List<City>> cityListMap = new HashMap<>();
        List<Map<String, Object>> aList = getList(new HashMap<>());
        if (aList != null && aList.size() > 0) {
            for (Map<String, Object> map : aList) {
                String province = map.get("province").toString();
                String provinceCode = map.get("provinceCode").toString();
                String city = map.get("city").toString();
                String cityCode = map.get("cityCode").toString();
                LmCmsAreas area = lmCmsAreasDao.detailByCode(cityCode);
                String firstChar = area.getFirstChar();
                List<City> cityList1 = cityListMap.get(firstChar);
                if (cityList1 == null || cityList1.size() < 1) {
                    cityList1 = new ArrayList<City>();
                }
                City city1 = new City();
                city1.setAreaCode(cityCode);
                city1.setAreaName(city);
                cityList1.add(city1);
                cityListMap.put(firstChar, cityList1);
            }
        }

        JSONArray data = new JSONArray();
        for (Map.Entry<String, List<City>> entry : cityListMap.entrySet()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("dataList", JSON.toJSONString(entry.getValue()));
            jsonObject.put("id", entry.getKey());
            data.add(jsonObject);
        }
        System.out.println(data);
    }

    public Result saveToRedis() {
        Map<String, String> cityListMap = new HashMap<>();
        List<Map<String, Object>> aList = getList(new HashMap<>());
        if (aList != null && aList.size() > 0) {
            for (Map<String, Object> map : aList) {
                String city = map.get("city").toString();
                String cityCode = map.get("cityCode").toString();
                redisClient.hset(CITYCODEHASH, cityCode, city);
                redisClient.hset(CITYNAMEHASH, city, cityCode);
            }
        }
        Result result = new Result();
        result.setCode(StatusCode.OK);
        result.setMsg(StatusCode.codeMsgMap.get(StatusCode.OK));
        return result;
    }


}
